package com.itcast.demoone.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.itcast.demoone.R

class MyAdapter(private val items: List<String>): RecyclerView.Adapter<MyAdapter.ViewHolder>() {

    /**
     * ViewHolder类，用于缓存视图项中的控件
     * @param view 单项视图布局
     */
    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        // 基于传递参数的视图(XML)获取要复用的控件
         val textView: TextView = view.findViewById(R.id.item_text)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_layout, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(
        holder: ViewHolder,
        position: Int
    ) {
       holder.textView.text = items[position]
    }

    override fun getItemCount(): Int {
        return items.size
    }
}